<?php

namespace App\Console\Commands;

use App\Model\Branch;
use App\Model\Order;
use App\Model\Order1;
use App\Model\PushOrder;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class xhPush extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'xhPush';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        while(true){
            try {
                //控制是否推单给下线
                $branch = Branch::where("id", 21)->first();
                if ($branch['tel'] == 1) {
                    $order = DB::select("select id,buytime,chargeaccount,remainingnumber,price,num,orderno from tiktok_pushorder where status = '未处理' order by id asc limit 1");
//                $order = DB::select("select id,buytime,chargeaccount,remainingnumber,num,orderno from tiktok_order1 where status = '未处理'   order by id asc limit 1");
                    $num = Branch::where("tel", 1)->count();
                    if (isset($order[0])) {
                        $order1 = $order[0];
                        $order = (array)$order1;
                        if ($order['id'] % $num == 0) {
                            $res = $this->xh_PushOrder($order['chargeaccount'], $order['orderno'], $order['remainingnumber']);
                            $info['ext'] = $res;
                            PushOrder::where("orderno", $order['orderno'])->update($info);
//                        Order1::where("orderno",$order['orderno'])->update($info);
                            $res = json_decode($res);
                            if ($res->code == 0) {
                                $info['status'] = '已处理';
                                $info['branch'] = '已推送,等待反馈';
                                $info['isok'] = 3;
                                $info['bid'] = 21;
                                $info['time'] = time();
                                PushOrder::where('orderno', $order['orderno'])->update($info);
                                Order::where('orderno', $order['orderno'])->update($info);
//                            Order1::where('orderno', $order['orderno'])->update($info);
                                $branch = Branch::where("id", 12)->first();
                                $orderInfo['num'] = $branch['num'] + 1;
                                $orderInfo['money'] = $branch['money'] + $order['remainingnumber'];
                                Branch::where("id", $branch['id'])->update($orderInfo);
//                                echo $order['id'];
                            }
                        }
                    }
                }
            } catch (\Exception $e) {
                echo $e->getMessage();
            }
            sleep(20);
        }
    }

    public function xh_PushOrder($dyid, $orderid, $money)
    {
        $sign = md5('agentid=51001&seckey=8BE38A6B7800412FA18F9E712642C2E6');
        $orders = [
            "cardno"    =>  $dyid,
            "location"  =>  "",
            "money"     =>  $money,
            "orderid"   =>  $orderid,
            "paytype"   =>  "快充"
        ];
        //JSON
        $orders = json_encode($orders,JSON_UNESCAPED_UNICODE);

        $orders = "[".$orders."]";

        $post = [
            'agentid' => 51001,
            'sign' => $sign,
            'orders' => $orders,
            'timestamp' => time()
        ];
        $json_post = json_encode($post,JSON_UNESCAPED_UNICODE);

        $res = curlPost('http://106.52.140.214:6491/DBOrderApi/CreateOrders', $json_post, $orderid);

        return $res;
    }
}
